//
//  WBStatusViewModel.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/14.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBStatusViewModel.h"
#import "WBDateTool.h"

@implementation WBStatusViewModel

- (instancetype)initWithStatusModel:(WBStatusModel *)model
{
    if(self = [super init])
    {
        self.statusModel = model;
        
        //给会员图片赋值
        self.membershipImage = [self selectmembershipImageWithMbrank:model.user.mbrank];
        
        //给用户认证图片赋值
        self.verifiedImage = [self selectedVerifiedImageWithVerified:model.user.verified];
        //转发
        self.repostsStr = [self setButtonTitleWithCount:model.reposts_count andTitle:@"转发"];
        //评论
        self.commentsStr = [self setButtonTitleWithCount:model.comments_count andTitle:@"评论"];
        //赞
        self.attitudesStr = [self setButtonTitleWithCount:model.attitudes_count andTitle:@"赞"];
        
        //微博来源
        self.sourceStr = [self handleSinaSource:model.source];
    }
    return self;
}

//重写get方法 相当于Swift里面的计算型属性
- (NSString *)sinaTime
{
    
    return [[WBDateTool sharedDateTool] handleTimeWithServerBackTimeStr:self.statusModel.created_at];

}

+ (instancetype)statusViewModelWithStatusModel:(WBStatusModel *)model
{
    
    return [[self alloc] initWithStatusModel:model];

}

//处理来源的字符串
- (NSString *)handleSinaSource:(NSString *)source
{
    //判断是否是包括">
    if([source containsString:@"\">"])
    {
        //包括计算range
        NSRange startRange = [source rangeOfString:@"\">"];
        
        //开始的下标
        int startIndex = (int)startRange.location + (int)startRange.length;
        
        NSRange endRange = [source rangeOfString:@"</"];
        
        //结束的下标
        int endIndex = (int)endRange.location;
        
        //截取的长度
        int len = endIndex - startIndex;
        
        return [NSString stringWithFormat:@"来自 %@",[source substringWithRange:NSMakeRange(startIndex, len)]];
        
        
    }
    
    return @"来自 那遥远的地方";
    
}



//返回字符串 给按钮设置  根据传过来的数据
- (NSString *)setButtonTitleWithCount:(int)count andTitle:(NSString *)title
{
    if(count <= 0)
    {
        return title;
    }else if (count < 10000)
    {
        return [NSString stringWithFormat:@"%d",count];
    }else
    {
        //- 如果 count%10000 == 0   示例 20000  显示  2万
        
        NSString *string = [NSString stringWithFormat:@"%.1f万",count/10000.0];
        
        if([string containsString:@".0万"])
        {
            //字符串替换
            string = [string stringByReplacingOccurrencesOfString:@".0万" withString:@"万"];
        }
        
        return string;
        
    }
    
}


//微博等级的确定 因为是topview要处理的数据逻辑，所以封装到这viewmodel来解决
- (UIImage *)selectmembershipImageWithMbrank:(int)mbrank
{
    // 处理微博等级image/// 会员等级 1-6
    if (mbrank > 0 && mbrank < 7)
    {
        
        return [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%d",mbrank]];
        
    }
    
    
    return [UIImage imageNamed:@"common_icon_membership_expired"];
}

//用户认证图片的选择 需要逻辑处理 封装到这里面使用
- (UIImage *)selectedVerifiedImageWithVerified:(int)verified
{
    // 处理微博用户认证image/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    if(verified == 1)
    {
        
        return [UIImage imageNamed:@"avatar_vip"];
        
    }else if (verified == 2 || verified == 3 || verified == 5)
    {
        
        return [UIImage imageNamed:@"avatar_enterprise_vip"];
        
    }else if (verified == 220)
    {
        
        return [UIImage imageNamed:@"avatar_grassroot"];
    }else
    {
        
        return [UIImage imageNamed:@"avatar_vgirl"];
    }
    
}


@end
